package com.world.business.constitution.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.world.business.constitution.domain.Constitution;
import com.world.business.constitution.domain.query.ConstitutionAddRequest;
import com.world.business.constitution.domain.query.ConstitutionRequest;
import com.world.business.constitution.domain.vo.ConstitutionVO;
import com.world.business.constitution.service.IConstitutionService;
import com.world.common.controller.BaseController;
import com.world.common.domain.PageResult;
import com.world.common.domain.ResponseData;
import com.world.common.domain.SuccessResponseData;
import com.world.common.enumeration.DelFlagEnum;
import com.world.common.exception.BusinessException;
import com.world.common.security.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 体质表 前端控制器
 * </p>
 *
 * @author 邓驴
 * @since 2023-07-27
 */
@Api(tags = "体质")
@RestController
@RequestMapping("/constitution")
public class ConstitutionController extends BaseController<Constitution> {

    @Resource
    private IConstitutionService constitutionService;


    /**
     * 分页列表
     *
     * @param request 请求参数
     * @return {@link ResponseData<PageResult>}
     * @author WangYX
     * @date 2023/09/07 16:04
     */
    @ApiOperation("分页")
    @GetMapping("/page")
    public ResponseData<PageResult<ConstitutionVO>> page(ConstitutionRequest request) {
        IPage<ConstitutionVO> page = constitutionService.getPage(startPage(request), request);
        return new SuccessResponseData<>(getPageResult(page));
    }


    /**
     * 列表
     *
     * @param request 请求参数
     * @return {@link ResponseData} 返回值
     * @author 邓驴
     * @date 2023/7/29 20:49
     **/
    @ApiOperation("列表")
    @GetMapping("/list")

    public ResponseData<List<ConstitutionVO>> list(ConstitutionRequest request) {
        LambdaQueryWrapper<Constitution> queryWrapper = Wrappers.lambdaQuery(Constitution.class);
        queryWrapper.eq(Constitution::getDelFlag, DelFlagEnum.NORMAL.getCode());
        queryWrapper.like(StrUtil.isNotBlank(request.getName()), Constitution::getName, request.getName());
        List<Constitution> list = constitutionService.list(queryWrapper);
        List<ConstitutionVO> vos = BeanUtil.copyToList(list, ConstitutionVO.class);
        return new SuccessResponseData<>(vos);
    }

    /**
     * 新增
     *
     * @param request 新增请求参数
     * @return {@link ResponseData}
     * @author 邓驴
     * @date 2023/7/29 21:03
     **/
    @ApiOperation("新增")
    @PostMapping("/add")
    public ResponseData<Long> add(@Validated @RequestBody ConstitutionAddRequest request) {
        //判断数据是否已经存在
        LambdaQueryWrapper<Constitution> queryWrapper = Wrappers.lambdaQuery(Constitution.class);
        queryWrapper.eq(Constitution::getDelFlag, DelFlagEnum.NORMAL.getCode());
        queryWrapper.eq(Constitution::getName, request.getName());
        Constitution one = constitutionService.getOne(queryWrapper);
        if (BeanUtil.isNotEmpty(one)) {
            throw new BusinessException(HttpStatus.HTTP_BAD_REQUEST, "数据已存在！");
        }
        //新增
        Constitution constitution = BeanUtil.copyProperties(request, Constitution.class);
        constitution.setCreateBy(SecurityUtils.getLoginUser().getName());
        constitution.setCreateById(SecurityUtils.getLoginUser().getId());
        constitution.setCreateTime(LocalDateTime.now());
        constitution.setDelFlag(DelFlagEnum.NORMAL.getCode());
        constitutionService.save(constitution);
        return new SuccessResponseData<>(constitution.getId());
    }


}
